package request;

import org.abacus.client.ui.view.login.LoginView;
import org.abacus.http.Response;
import org.abacus.http.StringHttpRequest;
import org.abacus.json.JsonObject;
import org.abacus.json.JsonRoot;
import org.abacus.json.v3.Json;


public abstract class AbstractActionRequest implements ActionRequest {
	protected boolean hasNext = false;

	protected static final String DEFAULT_REQUEST = "{}";

	static {
		JsonObject.format = false;
	}

	@Override
	public boolean hasNext() {
		return hasNext;
	}

	public void setHasNext(boolean hasNext) {
		this.hasNext = hasNext;
	}

	protected <T> T done(Object o, String uri, Class<T> clazz) {
		String req;
		if (o instanceof String) {
			req = (String) o;
		} else {
			org.abacus.json.Json j = new org.abacus.json.Json();
			JsonRoot jsonRoot = j.toJson(o);
			req = jsonRoot.toString();
		}
		StringHttpRequest stringHttpRequest = new StringHttpRequest(LoginView.getServerUrl() + uri + ".json");
		System.out.println("request: " + req);
		Response<String> s = stringHttpRequest.doWork(req);
		String res = s.getResponse();
		System.out.println("response: " + res);

		Json reader = new Json(res);
		T t = reader.build("data").get(clazz);

		// JsonReader reader = new JsonReader(res);
		// T t = reader.read("data", clazz);
		// Gson gson = new Gson();
		// T t = gson.fromJson("data", clazz);
		System.out.println("T = " + t);
		return t;
	}

}
